from .getPublicData import *
from myApp.models import JobInfo
import json


def getPageData():
    return hotCity


def getSalaryData(city):
    jobs = JobInfo.objects.filter(address=city)
    salaries = []
    for i in jobs:
        if i.practice == 0:
            salaries.append(json.loads(i.salary)[1])
    salaryColumn = [0 for x in range(len(salaryList))]  # 列表推导式？？？ 将salaryColumn的长度变得和salaryList相同，并且每个元素的值为0
    for i in salaries:
        s = i / 1000
        if s < 10:  # 如果s在0-10K这个区间
            salaryColumn[0] += 1
        elif s < 20:  # 如果s在10-20K这个区间
            salaryColumn[1] += 1
        elif s < 30:  # 如果s在20-30K这个区间
            salaryColumn[2] += 1
        elif s < 40:  # 如果s在30-40K这个区间
            salaryColumn[3] += 1
        elif s > 40:  # 如果s大于40K
            salaryColumn[4] += 1

    return salaryList, salaryColumn


def getCompanyPeopleData(city):
    jobs = JobInfo.objects.filter(address=city)
    people = []
    for i in jobs:
        people.append(json.loads(i.companyPeople)[1])
    peopleColumn = [0 for x in range(len(companyPeople))]
    for i in people:
        if i < 20:
            peopleColumn[0] += 1
        elif i < 100:
            peopleColumn[1] += 1
        elif i < 500:
            peopleColumn[2] += 1
        elif i < 1000:
            peopleColumn[3] += 1
        elif i < 10000:
            peopleColumn[4] += 1
        else:
            peopleColumn[5] += 1
    result = []
    """
        enumerate()可以返回索引值和元素
        比如peopleColumn=[0, 12, 6, 4, 14, 8]
        enumerate(peopleColumn)返回的结果就是
        0 0
        1 12
        2 6
        3 4
        4 14
        5 8
    """
    for index, item in enumerate(peopleColumn):
        result.append({
            'name': companyPeople[index],  # 通过索引值找到companyPeople中对应的元素，返回给result.name
            'value': item  # 然后把值直接传给result.value
        })
    return result


def getEducationData(city):
    jobs = JobInfo.objects.filter(address=city)
    educationData = {}
    for job in jobs:
        if educationData.get(job.educational, -1) == -1:
            educationData[job.educational] = 1
        else:
            educationData[job.educational] += 1
    result = []
    for k, v in educationData.items():
        result.append({
            'name': k,
            'value': v
        })
    return result


def getDist(city):
    jobs = JobInfo.objects.filter(address=city)
    distData = {}
    for job in jobs:
        if job.dist != '':
            if distData.get(job.dist, -1) == -1:
                distData[job.dist] = 1
            else:
                distData[job.dist] += 1
    result = []
    for k, v in distData.items():
        result.append({
            'name': k,
            'value': v
        })
    print(result)
    return result
